var app = getApp()
var CusBase64 = require('../../utils/base64.js'); 
Page({
  data: {
    true: true,
    signIn: [],
    publish :[],
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,  // tab切换 
    clientHeight: 0,
    isShowAhturoizeWarning: false,
  },

  onShow: function () {
    if (getApp().globalData.isFormShare == true) {
      getApp().globalData.isFormShare = false
      this.setData({
        isShowAhturoizeWarning: getApp().globalData.isShowAhturoizeWarning,
        currentTab:1
      })
    }
    this.setData({
      isShowAhturoizeWarning: getApp().globalData.isShowAhturoizeWarning
    })

    var that = this
    if (this.data.currentTab === 0) {
      wx.request({
        url: getApp().data.service + '/menu/menuList',
        data: {
          userId: getApp().globalData.userid,
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          if (Object.prototype.toString.call(res.data.data) != '[object Undefined]')           {
            that.setData({
              publish: changeDateFormat(res.data.data)
            })
          }
          else {
            that.setData({
              publish: []
            })
          }
        }
      })
    }
    else {
      wx.request({
        url: getApp().data.service + '/menu/myOrderList',
        data: {
          userId: getApp().globalData.userid,
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          if (Object.prototype.toString.call(res.data.data) != '[object Undefined]') {
            that.setData({
              signIn: changeDateFormat(res.data.data)
            })
          }
          else {
            that.setData({
              signIn: []
            })
          }
        }
      })
    }
  },

  //tab导航条切换事件
  bindChange: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    })

    if (this.data.currentTab === 0) {
      wx.request({
        url: getApp().data.service + '/menu/myPublishList',
        data: {
          userId: getApp().globalData.userid,
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          if (Object.prototype.toString.call(res.data.data) != '[object Undefined]') {
            that.setData({
              publish: changeDateFormat(res.data.data)
            })
          }
          else{
            that.setData({
              publish: []
            })
          }
        },
        complete: function () {
          wx.hideToast()
        }
      })
    }
    else {
      wx.request({
        url: getApp().data.service + '/menu/myOrderList',
        data: {
          userId: getApp().globalData.userid,
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)

          if (Object.prototype.toString.call(res.data.data) != '[object Undefined]') {
            that.setData({
              signIn: changeDateFormat(res.data.data)
            })
          }
          else {
            that.setData({
              signIn: []
            })
          }
        },
        complete: function () {
          wx.hideToast()
        }
      })
    }

  },
  switchNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })

      if (this.data.currentTab === 0) {
        wx.request({
          url: getApp().data.service + '/menu/myPublishList',
          data: {
            userId: getApp().globalData.userid,
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res)
            if (Object.prototype.toString.call(res.data.data) != '[object Undefined]') {
              that.setData({
                publish: changeDateFormat(res.data.data)
              })
            }
            else {
              that.setData({
                publish: []
              })
            }
          },
          complete: function () {
            wx.hideToast()
          }
        })
      }
      else {
        wx.request({
          url: getApp().data.service + '/menu/myOrderList',
          data: {
            userId: getApp().globalData.userid,
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res)
            if (Object.prototype.toString.call(res.data.data) != '[object Undefined]') {
              that.setData({
                signIn: changeDateFormat(res.data.data)
              })
            }
            else {
              that.setData({
                signIn: []
              })
            }
          },
          complete: function () {
            wx.hideToast()
          }
        })
      }
    }
  },

  turnToDetail: function(e) {
    console.log(e)
    wx.navigateTo({
      url: '../detail/detail?id=' + e.currentTarget.dataset.item.id + '&kind=' + this.data.currentTab + '&orderId=' + e.currentTarget.dataset.item.orderId,
    })
  },

  onLoad: function () {
    var that = this
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              getApp().globalData.isShowAhturoizeWarning = false
              that.setData({
                isShowAhturoizeWarning: false
              })
              wx.login({
                success: res => {
                  wx.request({
                    url: getApp().data.service + '/menu/login',
                    data: {
                      code: res.code,
                      nickName: CusBase64.CusBASE64.encoder(getApp().globalData.userInfo.nickName)
                    },
                    header: {
                      'content-type': 'application/json'
                    },
                    success: function (res) {
                      console.log(res)
                      getApp().globalData.userid = res.data.data.id
                      getApp().globalData.realName = CusBase64.CusBASE64.decoder(res.data.data.realName)
                    }
                  })
                }
              })
            }
          })
        }
        else {
          getApp().globalData.isShowAhturoizeWarning = true
          that.setData({
            isShowAhturoizeWarning: true
          })
        }
      }
    })

    //获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        var height = res.windowHeight - 104
        that.setData({
          clientHeight: height   //设备的高度等于scroll-view内容的高度
        })
        console.log(height)
      }
    })
  },
  

  bindGetUserInfo: function (e) {
    var that = this
    console.log(e.detail.userInfo)
    if (e.detail.userInfo) {
      getApp().globalData.userInfo = e.detail.userInfo
      wx.login({
        success: res => {
          wx.request({
            url: getApp().data.service + '/menu/login',
            data: {
              code: res.code,
              nickName: getApp().globalData.userInfo.nickName
            },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log(res)
              getApp().globalData.userid = res.data.data.id
              getApp().globalData.realName = res.data.data.realName
              getApp().globalData.isShowAhturoizeWarning = false
              that.setData({
                isShowAhturoizeWarning:false
              })
              console.log(that.data.isShowAhturoizeWarning)
            }
          })
        }
      })
    } else {
      //用户按了拒绝按钮
    }
  } 
  
})

function changeDateFormat(arr) {
  if (Object.prototype.toString.call(arr) === '[object Array]')
  {
    for (var i = 0, len = arr.length; i < len; i++) {
      var startTime = arr[i].startTime
      arr[i].startTime = formatTime(startTime, 'Y-M-D h:m')
      var endTime = arr[i].endTime
      arr[i].endTime = formatTime(endTime, 'Y-M-D h:m')
      arr[i].title = CusBase64.CusBASE64.decoder(arr[i].title)
      arr[i].desc = CusBase64.CusBASE64.decoder(arr[i].desc)
    }
    console.log(11111)
    console.log(arr)
  }
  console.log(222222)
  return (arr)
}

function formatTime(number, format) {

  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

function formatNumber (n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}


